/*
 * File     : StructureByReferenceParameter.java
 * Created  : 3 May 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.call;

import static com.googlecode.dni.internal.DniTypes.ABSTRACT_STRUCTURE_TYPE;
import static com.googlecode.dni.internal.DniTypes.STRUCTURE_HELPER_TYPE;
import static com.googlecode.dni.internal.DniTypes.STRUCTURE_TYPE;
import static org.objectweb.asm.Opcodes.*;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import com.googlecode.dni.internal.DniInternal;
import com.googlecode.dni.internal.structure.StructureRecord;
import com.googlecode.dni.type.structure.Structure;

/**
 * <p>
 *  Encapsulates a parameter to a native call that comes from a
 *  {@link Structure} by reference.
 * </p>
 *
 * @author Matthew Wilson
 */
final class StructureByReferenceParameter extends NativeObjectParameter
{

    private String structureImplName;

    private String structureFactoryImplName;


    /** {@inheritDoc} */
    @Override
    void setJavaType( final Class< ? > javaType )
    {
        super.setJavaType( javaType );

        // ensure the structure exists, is valid, and has been loaded
        Class< ? extends Structure > structureType = javaType.asSubclass( Structure.class );
        StructureRecord structureRecord = DniInternal.getStructureRecord( structureType );
        this.structureImplName = structureRecord.getImplName();
        this.structureFactoryImplName = structureRecord.getFactoryImplName();
    }

    /** {@inheritDoc} */
    @Override
    void writeGetParameter( final MethodVisitor methodVisitor )
    {
        methodVisitor.visitVarInsn( ALOAD, getJavaVarIndex() );
        // cast against the precise type to:
        // a) prevent clients doing anything dodgy with their own types
        // b) prove to HotSpot that it is an exact cast that will always
        //    succeed
        methodVisitor.visitTypeInsn( CHECKCAST, this.structureImplName );

        methodVisitor.visitMethodInsn( INVOKESTATIC,
                                       STRUCTURE_HELPER_TYPE,
                                       "getAddress",
                                       "(L" + ABSTRACT_STRUCTURE_TYPE + ";)J" );

        if ( getNativeType() == 'I' )
        {
            methodVisitor.visitInsn( L2I );
        }
    }

    /** {@inheritDoc} */
    @Override
    void writeReturnParameter( final MethodVisitor methodVisitor, final int returnVarIndex )
    {
        methodVisitor.visitFieldInsn( GETSTATIC,
                                      this.structureFactoryImplName,
                                      "INSTANCE",
                                      "L" + this.structureFactoryImplName + ";" );
        if ( getNativeType() == 'I' )
        {
            methodVisitor.visitVarInsn( ILOAD, returnVarIndex );

            methodVisitor.visitMethodInsn( INVOKEVIRTUAL,
                                           this.structureFactoryImplName,
                                           "fromPointer",
                                           "(I)L" + STRUCTURE_TYPE + ";" );
        }
        else
        {
            assert getNativeType() == 'J';

            methodVisitor.visitVarInsn( LLOAD, returnVarIndex );

            methodVisitor.visitMethodInsn( INVOKEVIRTUAL,
                                           this.structureFactoryImplName,
                                           "fromPointer",
                                           "(J)L" + STRUCTURE_TYPE + ";" );
        }

        methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, this.structureImplName );

        methodVisitor.visitInsn( ARETURN );
    }

}
